<?php
namespace Admin\Controller;

use Admin\Controller\CommonController;

class InfoController extends CommonController
{

    private $infoModel;
    private $infoListModel;
    private $infoFlagModel;
    private $infoClassModel;


    public function __construct()
    {
        parent::__construct();
        $this->infoListModel = M('Infolist');
        $this->infoModel = M('info');
        $this->infoFlagModel = M('infoflag');
        $this->infoClassModel = M('infoclass');
    }

    /**
     * 单页信息管理
     */
    public function info()
    {
        $this->display();
    }


    /**
     * 获取单页信息管理
     */
    public function getInfoData()
    {
        $data = $this->infoModel
                ->alias('i')
                ->join('left join __INFOCLASS__ c on i.classid = c.id ')
                ->field('i.id,i.posttime,c.classname')
            ->order('id asc')->select();
        foreach($data as $k=>$v){
            $data[$k]['posttime'] = date('Y-m-d H:i:s',$v['posttime']);
        }
        $this->ajaxReturn($data);
    }

    /**
     * 查看修改单页信息页面
     */
    public function  addInfo()
    {
        $id = I('request.id'.'');
        if($id){
            $row = $this->infoModel->where('id='.$id)->find();
            $row['classname'] = $this->infoClassModel->where('id='.$row['classid'])->getField('classname');
            $row['time'] = date('Y-m-d H:i:s',$row['posttime']);
            $this->assign('row',$row);
        }
        $this->assign('id',$id);
        $this->display();
    }

    /**
     *修改单页信息管理
     * 单页信息只能在添加栏目时新增
     */
    public function doAddInfo()
    {
        $data = array();
        $data['posttime'] = strtotime(I('request.birthday'));
        $data['content'] = I('request.content');
        $data['picurl'] = I('request.picurl');
        $map = array();
        $map['id'] = I('request.id');

        if (!empty($map)) {
            $res = $this->infoModel->where($map)->save($data);
            if ($res) {
                $this->ajaxReturn(array('success' => 1, 'msg' => '数据更新成功'));
            } else {
                $this->ajaxReturn(array('success' => 0, 'msg' => '数据更新失败'));
            }
        } else {
            $this->ajaxReturn(array('success' => -1, 'msg' => '没有id'));
        }

    }


    /**
     * 列表信息管理
     */
    public function infoList()
    {
        $this->display();
    }

    /**
     * 获取列表管理
     */
    public function getInfoList()
    {
        $data = $this->infoListModel
            ->alias('i')
            ->join('left join __INFOCLASS__ c on i.classid = c.id ')
            ->field('i.id,i.posttime,c.classname,i.title,i.hits,i.author')
            ->order('id asc')->select();
        foreach($data as $k=>$v){
            $data[$k]['posttime'] = date('Y-m-d H:i:s',$v['posttime']);
        }
        $this->ajaxReturn($data);
    }

    /**
     *新增修改列表管理页面
     */
    public function addInfoList()
    {
        $html = "";
        $flag = $this->infoFlagModel->order('orderid asc')->select();
        foreach ($flag as $k => $v) {
            $html .= "<span><input type='checkbox' name='flag[]' id='flag[]' value=" . $v['flag'] . ">" . $v['flagname'] . $v['flag'] . "</span>";
        }
        $id = I('request.id','');
        if($id){
            $row = $this->infoListModel->where("id=" . $id)->field('picarr,flag')->find();
            $row['flag'] = explode(',',$row['flag']);

            $html = "";
            foreach ($flag as $k => $v) {
                if(in_array($v['flag'],$row['flag'])){
                    $checked = "checked";
                }else{
                    $checked = "";
                }
                $html .= "<span><input type='checkbox' name='flag[]' id='flag[]' value=".$v['flag']." $checked>" . $v['flagname'] . $v['flag'] . "</span>";
            }

            $picarr = unserialize($row['picarr']);
            foreach ($picarr as $k => $v) {
                $v = explode(',', $v);
                $picarr[$k] = $v;
            }
            $this->assign('id',$id);
            $this->assign('picarr', $picarr);
        }
        $this->assign('html', $html);
        $this->display();
    }

    /**
     * easyui form加载表单数据
     */
    public function getInfoListDetail(){
        $id = I('request.id',1);
        $data = $this->infoListModel->where('id='.$id)->find();
        $data['birthday'] = date('Y-m-d H:i:s',$data['posttime']);
        $data['flag'] = explode(',',$data['flag']);
        $this->ajaxReturn($data);
    }

    /**
     * 新增修改栏目列表信息
     */
    public  function doAddInfoList(){
        $data = array();
        $data = I('post.');
        $data['flag'] = implode(',',$data['flag']);
        $data['posttime'] = strtotime($data['birthday']);
        $picarr = $this->picarrBuild();

        //
        unset($data['birthday']);
        unset($data['picarr']);
        unset($data['picarr_txt']);

        $data['picarr'] = $picarr;
        //存在id修改
        if($data['id']){
            $res = $this->infoListModel->where('id='.$data['id'])->save($data);
        }else{
            $res = $this->infoListModel->add($data);
        }


        if ($res) {
            $this->ajaxReturn(array('success' => 1, 'msg' => '数据更新成功'));
        } else {
            $this->ajaxReturn(array('success' => 0, 'msg' => '数据更新失败'));
        }
    }

    /**
     * 生成图片数组字符串
     */
    private function picarrBuild()
    {

        $picarr = I('request.picarr', array());
        $picarr_txt = I('request.picarr_txt', array());
        $picarrNum = count($picarr);
        $picarrTmp = '';
        for ($i = 0; $i < $picarrNum; $i++) {
            $picarrTmp[] = $picarr[$i] . ',' . $picarr_txt[$i];
        }
        $picarr = serialize($picarrTmp);
        return $picarr;
    }


    /**
     *栏目管理
     */
    public function  infoClass()
    {
        $this->display();
    }

    /**
     * 获取栏目管理
     */
    public function getInfoClass()
    {
        $data = $this->infoClassModel->field('id,parentid ,classname,orderid')->select();
//        foreach($data as $k=>$v){
//            $data[$k]['_parentId'] = $v['parentid'];
//
//            if($v['parentid'] == 0){
//                $data[$k]['_parentId'] = null;
//            }
//
//
//                unset($data[$k]['parentid']);
////            if($v['parentid'] == 0){
////                $data[$k]['state'] = 'closed';
////            }else{
////                $data[$k]['state'] = 'open';
////            }
//        }
//        $list['rows'] = $data;
//        $list['total'] = count($data);
//        $this->ajaxReturn($list);
        if($data){
            $data = $this->get_attr($data,0);
            $this->ajaxReturn($data);
        }
    }


    /**
     * 新增修改栏目管理页面
     */
    public function addInfoClass()
    {
        $id = I('request.id','');
        if($id){
            $this->assign('id',$id);
        }
        $this->display();
    }

    /**
     *新增栏目管理
     */
    public function doAddInfoClass()
    {
        $data = array();
        $data = I('post.');


        //存在id时新增修改
        if($data['id']){
            $res = $this->infoClassModel->where('id = '.$data['id'])->save($data);
        }else{
            $res = $this->infoClassModel->add($data);
        }



        //新增栏目管理时，当栏目类型选择单页时，新增单页信息管理
        if($data['infotype'] == 1 && empty($data['id'])){
            $list = array();
            $list['classid'] = $res;
            $list['posttime'] = time();
            $return  = $this->infoModel->add($list);
        }

        if ($res !== false) {
            $this->ajaxReturn(array('success' => 1, 'msg' => '数据更新成功'));
        } else {
            $this->ajaxReturn(array('success' => 0, 'msg' => '数据更新失败'));
        }

    }

    /**
     * 获取栏目信息
     */
    public function getInfoClassDetail(){
        $id = I('request.id',1);
        $data = $this->infoClassModel->where('id='.$id)->find();
        $this->ajaxReturn($data);
    }


    /**
     *获取下拉框树状结构
     */
    public function getClassLevel()
    {

        $data = $this->infoClassModel->field('id,parentid,classname as text')->select();
        if (!empty($data) && count($data) >= 1) {
            $data = $this->get_attr($data, 0);
            
//            unset($data[0]['parentid']);
            $num = count($data);
            $data[$num]['id'] = 0;
            $data[$num]['text'] = '一级栏目';
            $data = array_reverse($data);
            $this->ajaxReturn($data);
        } else {
            $data = array();
            $data[0]['id'] = 0;
            $data[0]['text'] = "一级栏目";
            $this->ajaxReturn($data);
        }
    }

    /**
     * @param $a
     * @param $pid
     * @return array
     * 无限分类
     */
    private function get_attr($a, $pid)
    {
        $tree = array();                                //每次都声明一个新数组用来放子元素
        foreach ($a as $v) {
            if ($v['parentid'] == $pid) {                      //匹配子记录
                $v['children'] = $this->get_attr($a, $v['id']); //递归获取子记录
                if ($v['children'] == null) {
                    unset($v['children']);             //如果子元素为空则unset()进行删除，说明已经到该分支的最后一个元素了（可选）
                }
                $tree[] = $v;                           //将记录存入新数组
            }
        }
        return $tree;                                  //返回新数组
    }


    /**
     *信息标记管理
     */
    public function  infoFlag()
    {
        $this->display();
    }


    /**
     * 获取信息标记管理
     */
    public function  getInfoFlag()
    {
        $data = $this->infoFlagModel->order('orderid asc')->select();
        $this->ajaxReturn($data);
    }

    /**
     *新增信息标记管理
     */
    public function  addInfoFlag()
    {
        $data = array();
        $flagname = I('request.flagname');
        $flag = I('request.flag');
        $orderid = I('request.orderid');
        $action = I('request.action');
        if ($flagname == "" or $flag == "") {
            $this->ajaxReturn(array('status' => 0, 'msg' => '属性数据不能为空'));
        }
        //判断级联
        $isWhere = "flagname = '{$flagname}' or flag = '{$flag}'";
        if ($action == "update") {
            $id = I('request.id');
            $isWhere = "(" . $isWhere . ") and id !=" . $id;
        }
        $row = $this->infoFlagModel->where($isWhere)->find();
        if (is_array($row)) {
            if ($row['flagname'] == $flagname) {
                $this->ajaxReturn(array('status' => 0, 'msg' => '级联名称已存在'));
            }
            if ($row['flag'] == $flag) {
                $this->ajaxReturn(array('status' => 0, 'msg' => '级联标识已存在'));
            }
        }
        //组成数组
        $data['flagname'] = $flagname;
        $data['flag'] = $flag;
        $data['orderid'] = $orderid;
        if ($action == "update") {
            $map['id'] = $id;
            $res = $this->infoFlagModel->where($map)->save($data);
        } else {
            $res = $this->infoFlagModel->add($data);
        }
        if ($res) {
            $this->ajaxReturn(array('success' => 1, 'msg' => '数据更新成功', 'id' => $id));
        } else {
            $this->ajaxReturn(array('success' => 0, 'msg' => '数据更新失败'));
        }
    }


}